import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import '../../models/contract_model.dart';
import 'contract_detail_controller.dart';

class ContractDetailPage extends GetView<ContractDetailController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('合同详情'),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          onPressed: () => Get.back(),
        ),
      ),
      body: Obx(() {
        if (controller.isLoading.value) {
          return const Center(child: CircularProgressIndicator());
        }

        final contract = controller.contract.value;
        return SingleChildScrollView(
          padding: const EdgeInsets.all(24),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _buildInfoCard('基本信息', [
                _buildInfoItem('合同编号', contract.contractNumber),
                _buildInfoItem('客户名称', contract.customerName),
                _buildInfoItem('项目名称', contract.projectName),
                _buildInfoItem('签订日期',
                    DateFormat('yyyy-MM-dd').format(contract.contractDate)),
                _buildInfoItem('到期日期',
                    DateFormat('yyyy-MM-dd').format(contract.expirationDate)),
                _buildInfoItem('合同金额', '¥${contract.contractAmount}'),
                _buildInfoItem('合同类型', contract.contractType),
                _buildInfoItem('付款方式', contract.paymentMethod),
              ]),
              const SizedBox(height: 16),
              _buildInfoCard('其他信息', [
                _buildInfoItem('付款条款', contract.paymentTerms),
                _buildInfoItem('备注', contract.remarks),
                _buildStatusItem(contract),
              ]),
              const SizedBox(height: 24),
              _buildActionButtons(contract),
            ],
          ),
        );
      }),
    );
  }

  Widget _buildInfoCard(String title, List<Widget> children) {
    return Card(
      elevation: 0,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(8),
      ),
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              title,
              style: const TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.bold,
              ),
            ),
            const Divider(height: 24),
            ...children,
          ],
        ),
      ),
    );
  }

  Widget _buildInfoItem(String label, String value) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Expanded(
            flex: 2,
            child: Text(
              label,
              style: TextStyle(
                color: Colors.grey[600],
                fontSize: 14,
              ),
            ),
          ),
          const SizedBox(width: 16),
          Expanded(
            flex: 3,
            child: Text(
              value,
              style: const TextStyle(
                fontSize: 14,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildStatusItem(Contract contract) {
    String status;
    Color color;
    if (contract.isAbandoned ?? false) {
      status = '已放弃';
      color = Colors.red;
    } else if (contract.isRenewed ?? false) {
      status = '已续约';
      color = Colors.green;
    } else {
      final diff = contract.expirationDate.difference(DateTime.now()).inDays;
      status = diff >= 0 ? '剩余$diff天' : '已过期';
      color = diff >= 0 ? Colors.orange : Colors.red;
    }

    return Container(
      margin: const EdgeInsets.only(top: 8),
      padding: const EdgeInsets.symmetric(vertical: 8),
      child: Row(
        children: [
          Text(
            '合同状态：',
            style: TextStyle(
              color: Colors.grey[600],
              fontSize: 14,
            ),
          ),
          const SizedBox(width: 8),
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
            decoration: BoxDecoration(
              color: color.withOpacity(0.2),
              borderRadius: BorderRadius.circular(12),
            ),
            child: Text(
              status,
              style: TextStyle(
                color: color,
                fontSize: 14,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildActionButtons(Contract contract) {
    return Obx(() {
      final isOperating = controller.isLoading.value;
      return Column(
        children: [
          if (!(contract.isRenewed ?? false) &&
              !(contract.isAbandoned ?? false))
            Row(
              children: [
                Expanded(
                  child: TextButton(
                    onPressed: isOperating ? null : controller.handleRenew,
                    style: TextButton.styleFrom(
                      padding: const EdgeInsets.symmetric(vertical: 16),
                      backgroundColor: Colors.blue,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(8),
                      ),
                    ),
                    child: isOperating
                        ? const CircularProgressIndicator(color: Colors.white)
                        : const Text(
                            '续约',
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 16,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                  ),
                ),
                const SizedBox(width: 16),
                Expanded(
                  child: TextButton(
                    onPressed: isOperating ? null : controller.handleAbandon,
                    style: TextButton.styleFrom(
                      padding: const EdgeInsets.symmetric(vertical: 16),
                      backgroundColor: Colors.grey[300],
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(8),
                      ),
                    ),
                    child: isOperating
                        ? const CircularProgressIndicator()
                        : const Text(
                            '放弃续约',
                            style: TextStyle(
                              color: Colors.black87,
                              fontSize: 16,
                            ),
                          ),
                  ),
                ),
              ],
            ),
          if (contract.isRenewed ?? false)
            TextButton(
              onPressed: null,
              style: TextButton.styleFrom(
                padding: const EdgeInsets.symmetric(vertical: 16),
                backgroundColor: Colors.green.withOpacity(0.2),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(8),
                ),
              ),
              child: const Text(
                '已续约',
                style: TextStyle(
                  color: Colors.green,
                  fontSize: 16,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          if (contract.isAbandoned ?? false)
            TextButton(
              onPressed: null,
              style: TextButton.styleFrom(
                padding: const EdgeInsets.symmetric(vertical: 16),
                backgroundColor: Colors.red.withOpacity(0.2),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(8),
                ),
              ),
              child: const Text(
                '已放弃续约',
                style: TextStyle(
                  color: Colors.red,
                  fontSize: 16,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
        ],
      );
    });
  }
}
